﻿namespace Guia.Elements.PatternServants
{
    using System.Windows.Automation;
    using Guia.Core;

    /// <summary>
    /// Represents the implementation for the ExpandCollapse-Pattern.
    /// </summary>
    public class ExpandCollapseImpl : AbstractPatternServant, IExpandCollapse
    {
        /// <summary>
        /// Initializes a new instance of the ExpandCollapseImpl class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the UiElement.</param>
        public ExpandCollapseImpl(UiElement automationElement)
            : base(automationElement)
        {
        }

        #region IExpandCollapse Members

        /// <summary>
        /// Gets or sets a value indicating whether the state of this ComboBox is expanded or not.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception may be thrown when the element is disabled.</exception>
        public bool IsExpanded
        {
            get
            {
                ExpandCollapseState expandCollapseState = this.UiElement.GetPattern<ExpandCollapsePattern>().Current.ExpandCollapseState;
                return expandCollapseState == ExpandCollapseState.Expanded;
            }

            set
            {
                if (this.IsExpanded && value == false)
                {
                    this.Collapse();
                }
                else if (!this.IsExpanded && value)
                {
                    this.Expand();
                }
            }
        }

        /// <summary>
        /// Expands the ComboBox
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the element is disabled.</exception>
        public void Expand()
        {
            if (!this.UiElement.IsEnabled)
            {
                throw new OperationNotSupportedException("Can not collapse item because it's not enabled");
            }

            this.UiElement.GetPattern<ExpandCollapsePattern>().Expand();
        }

        /// <summary>
        /// Collapses the ComboBox
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the element is disabled.</exception>
        public void Collapse()
        {
            if (!this.UiElement.IsEnabled)
            {
                throw new OperationNotSupportedException("Can not collapse item because it's not enabled");
            }

            this.UiElement.GetPattern<ExpandCollapsePattern>().Collapse();
        }

        #endregion
    }
}
